//
//  OperationViewController.m
//  HealthA段
//
//  Created by MrJXT on 15/6/8.
//  Copyright (c) 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "OperationViewController.h"
#import "OperationTab.h"
#import "OperationTableCell.h"
#import "AFHTTPRequestOperationManager.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"
#import "OperationDatilViewController.h"
#define HEIGHT self.view.frame.size.height
#define WIDTH self.view.frame.size.width
@interface OperationViewController ()<UITableViewDataSource,UITableViewDelegate>
//手术信息列表视图
@property(nonatomic,retain)UITableView *OperationTable;
//手术信息列表数组
@property(nonatomic,retain)NSMutableArray *OperationArr;
@property(nonatomic,assign)NSInteger page;
@end

@implementation OperationViewController
- (void)dealloc
{
    [_OperationArr release];
    [_OperationTable release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.OperationArr = [NSMutableArray array];
        self.page = 1;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    [self creatOperationTableView];
    [self PleaseData];
    [self method];
}
//创建手术信息列表视图
-(void)creatOperationTableView
{
    self.OperationTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) style:UITableViewStylePlain];
    [self.view addSubview:self.OperationTable];
    self.OperationTable.delegate = self;
    self.OperationTable.dataSource = self;
    self.OperationTable.rowHeight = 160;
    [_OperationTable release];
    self.OperationTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

//    self.OperationTable.separatorStyle = UITableViewCellSeparatorStyleNone;
}
#pragma mark 实现协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.OperationArr count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *resue = @"reuse";
    OperationTableCell *cell = [tableView dequeueReusableCellWithIdentifier:resue];
    if (cell == nil) {
        cell = [[[OperationTableCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:resue]autorelease];
    }
    OperationTab *op = self.OperationArr[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"http://www.yi18.net/%@",op.img];
    NSURL *url = [NSURL URLWithString:str];
    [cell.picView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"占位图"]];
    cell.namelabel.text = op.name;
    cell.departLabel.text = [NSString stringWithFormat:@"科室:%@",op.department];
    cell.placeLabel.text = [NSString stringWithFormat:@"部位:%@",op.place];
//    cell.countLabel.text = [NSString stringWithFormat:@"浏览:%@次",op.count];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
//请求手术信息列表数据
-(void)PleaseData
{
    NSString *url = [NSString stringWithFormat:@"http://api.yi18.net/surgery/list?page=%ld&limit=20&pid=%ld",self.page,self.did];
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"yi18"];
        for (NSMutableDictionary *temp in arr) {
            OperationTab *optb = [[OperationTab alloc]init];
            [optb setValuesForKeysWithDictionary:temp];
            [self.OperationArr addObject:optb];
            [optb release];
        }
        [self.OperationTable reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"手术信息列表数据请求失败");
    }];
    self.page++;
}
//上啦加载
- (void)method
{
    [self.OperationTable addLegendHeaderWithRefreshingBlock:^{
        [self.OperationArr removeAllObjects];
        self.page = 1;
        [self PleaseData];
        [self.OperationTable reloadData];
        [self.OperationTable.header endRefreshing];
    }];
    [self.OperationTable.legendHeader beginRefreshing];
    [self.OperationTable addLegendFooterWithRefreshingBlock:^{
        self.page++;
        [self PleaseData];
        [self.OperationTable reloadData];
        [self.OperationTable.footer endRefreshing];
    }];
}
//跳转手术信息详情列表
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OperationDatilViewController *odvc = [[OperationDatilViewController alloc]init];
    OperationTab *op = self.OperationArr[indexPath.row];
    odvc.id = op.id;
    odvc.title = op.name;
    NSLog(@"手术信息详情ID===%ld",odvc.id);
    [self.navigationController pushViewController:odvc animated:YES];
    [odvc release];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
